package com.icebartech.core.local;

/**
 * 统一用户对象threadlocal类，每个线程绑定一个登录用户，注意该类禁止被继承
 * @author istrator
 *
 */
public final class UserThreadLocal {

	private static ThreadLocal<LocalUser> localUserThreadLocal = new ThreadLocal<>();
	
	/**
	 * 设置登录用户
	 * @param user
	 */
	public static void setUserInfo(LocalUser user){
		localUserThreadLocal.set(user);
	}
	
	/**
	 * 获取当前登录用户
	 * @return
	 */
	public static LocalUser getUserInfo(){
		return localUserThreadLocal.get();
	}
	
	/**
	 * 移除掉登录用户id
	 */
	public static void removeUserInfo(){
		localUserThreadLocal.remove();
	}
	
	/**
	 * 获取当前登陆用户Id
	 * @return
	 */
	public static Long getUserId(){
		LocalUser localUser = localUserThreadLocal.get();
		return null == localUser ? null : localUser.getUserId();
	}
}